package com.llk.pool.controller.api;

import com.llk.pool.domain.User;
import com.llk.pool.mapper.UserMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController("/api/")
public class ApiUserController {

    Logger logger = LoggerFactory.getLogger(ApiUserController.class);

    @Autowired
    private UserMapper userMapper;


    @ApiOperation(value = "获取用户列表", notes = "本url用来获取用户列表")
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public List<User> getUsers() {

        logger.info("GET Method Coming");
        logger.info("********" + (null == userMapper) + "********");
        List<User> list = userMapper.getAll();

        if (null != list)
            return list;
        else
            return new ArrayList<User>();
    }
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path", defaultValue = "1")
    @RequestMapping(value = "users/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable("id") Long id) {
        User user = userMapper.getOne(id);
        return user;
    }

    /**
     * 新增user : userName, passWord, userSex, nickName
     *
     * @param user
     * @return
     */

    @ApiOperation(value = "新增一个用户", notes = "根据User对象创建用户")
    @RequestMapping(value = "users", method = RequestMethod.POST)
    public String add(User user) {
        logger.info(user.toString());
        userMapper.insert(user);
        return "success";
    }

    @ApiOperation(value = "更新某一用户", notes = "更新用户详细信息, 根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long" ,paramType = "path")
    @RequestMapping(value = "users/{id}", method = RequestMethod.PUT)
    public String update(@PathVariable("id") Long id,  User user, HttpServletRequest request) {
        logger.info("-----" + request.getParameter("userName"));
        user.setId(id);
        logger.info(user.toString());
        userMapper.update(user);
        return "success";
    }


    @ApiOperation(value = "删除某一用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long" ,paramType = "path")
    @RequestMapping(value = "users/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Long id) {
        userMapper.delete(id);
        return "success";
    }


    /**
     * 在HTTP中，PUT被定义为idempotent的方法，POST则不是，这是一个很重要的区别。
     * 上面的话就是说，如果一个方法重复执行多次，产生的效果是一样的，那就是idempotent的
     * 比如说put多次结果是一样的没有产生别的副作用就用put.如果多次请求产生的结果不一样就用POST
     * @param id
     * @param name
     * @return
     */
//    @RequestMapping(value = "/user",method = RequestMethod.PUT)
//    public User updateUser(@RequestParam(value = "id",defaultValue = "8") int id,
//                           @RequestParam(value = "name",defaultValue = "Mrchang") String name) {
//        // TODO: 17-4-28
//        return  null;
//    }

}
